﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PPIDesktopClient.Entities;
using PPIDesktopClient.General;
using VMind.Global;
using PPIProxy;
using System.Data;
using System.Collections;

namespace PPIDesktopClient.Logic
{
    public class LHandheld
    {
        public static List<EHandheld> SelectAllIdName()
        {
            ConfigReader _Reader = ConfigReader.GetInstance();
            List<EHandheld> _handhelds = new List<EHandheld>();
            DataTable _dt = WHandheld.SelectAllIdName(_Reader.GetConnectionString()).Tables[0];
            EHandheld _handheld;

            foreach (DataRow  dr in _dt.Rows)
            {
                _handheld = new EHandheld(ConvertHelper.ToInt32(dr["HandheldId"]), ConvertHelper.Tostring(dr["Name"]));
                _handhelds.Add(_handheld);
            }
            return _handhelds;
        }

        public static int Insert(EHandheld handheld)
        {
            ConfigReader _Reader = ConfigReader.GetInstance();
            handheld.Id = WHandheld.Insert(handheld.Name,_Reader.GetConnectionString());
            return handheld.Id;
        }
        public static EHandheld Select(int id)
        {
            try
            {
                ConfigReader _Reader = ConfigReader.GetInstance();
                DataTable _dt = WHandheld.Select(id,_Reader.GetConnectionString()).Tables[0];
                if (_dt.Rows.Count > 0)
                {
                    DataRow dr = _dt.Rows[0];
                    EHandheld tmpHandheld = new EHandheld(ConvertHelper.ToInt32(dr["HandheldId"]), dr["Name"].ToString());
                    return tmpHandheld;
                }
                else
                {
                    throw new Exception();
                }

            }
            catch (Exception ex)
            {
                EHandheld tmpHandheld = new EHandheld();
                return tmpHandheld;
            }
            
        }
    }
}
